﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace chapter1._6Web.Handlers
{
    /// <summary>
    /// CreateCarsHanlers 的摘要说明
    /// </summary>
    public class CreateCarsHanlers : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //获取当前请求的url地址
            var currUrl = context.Request.RawUrl;
            int hIndex = currUrl.LastIndexOf("_");
            int portIndex = currUrl.LastIndexOf(".");
            Regex reg = new Regex(@"\w+_\d+\.html");
            int cardNum = int.Parse(currUrl.Substring(hIndex + 1, portIndex - hIndex - 1));
            //当前访问的汽车详情页面的物理地址
            string path = context.Server.MapPath("~/cars/carInfo") + $"_{cardNum}.html";
            //判断当前的汽车详情页是否存在,不存在生成对应的html文件
            //File.Exists(path):路径存在返回true,否则返回false
            if (!File.Exists(path))
            {
                using (EFC6Entities db = new EFC6Entities())
                {
                    //从数据库获取所有汽车集合
                    var carList = db.cards.ToList();
                    //获取模板的路径
                    string templatePath = context.Server.MapPath("~/cars/Template.html");
                    //从模板中读取所有内容
                    string tempHtml = ReadTemp(templatePath);
                    //用真实数据替换模板中对应占位符
                    tempHtml = tempHtml.Replace("{$pic}", carList[cardNum].cardPic);
                    tempHtml = tempHtml.Replace("{$cardName}", carList[cardNum].cardName);
                    tempHtml = tempHtml.Replace("{$cardBrand}", carList[cardNum].cardBrand);
                    //生成对应的html文件
                    WriteHtml(path, tempHtml);
                }
            }
            //将静态html页面发送给客户端替换原有页面
            context.Response.WriteFile(path);
        }
        //生成对应的html文件
        private void WriteHtml(string path, string tempHtml)
        {
            //创建文件流
            FileStream fs = new FileStream(path, FileMode.Create);
            //创建读写器
            StreamWriter sw = new StreamWriter(fs);
            //将文件写入到文件流
            sw.Write(tempHtml);
            //关闭读写器
            sw.Close();
            //关闭文件流
            fs.Close();
        }

        //读取文件内容
        private string ReadTemp(string templatePath)
        {
            //判断模板文件是否存在
            if (!File.Exists(templatePath))
            {
                //不存抛出异常，throw抛出异常会终止程序往下走
                throw new Exception("模板文件不存在");
            }
            //创建文件流
            FileStream fs = new FileStream(templatePath, FileMode.Open);
            //创建文件读写器
            StreamReader sr = new StreamReader(fs);
            //从头读到尾保存到字符串中
            string tempHtml = sr.ReadToEnd();
            //关闭读写器
            sr.Close();
            //关闭文件流
            fs.Close();
            //返回读取到的模板的所有内容
            return tempHtml;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}